<template>
    <div class="container">
        <div class="shop" ref="scroll">
            <div class="shop-wrapper">
                <div class="header">
                    <div class="bg"></div>
                    <div class="header-wrapper">
                        <div class="logo"></div>
                        <div class="title">沙县大酒店（朝阳店）</div>
                        <div class="time">
                            <div class="tag">预定</div>
                            <div class="t">营业中（10:00-23:00）</div>
                        </div>
                        <div class="address">
                            <span>广西南宁市西乡塘区科园大道82号万和大厦510室</span>
                        </div>
                    </div>
                </div>
                <div class="tab">
                    <div class="tab-header" ref="tabTitle">
                        <div class="item" v-for="(item, index) in tabs" :key="index" :class=" current === index ? 'current' : null " @click="tabClick(index)">
                            <span>{{item.title}}</span>
                        </div>
                    </div>
                    <div class="tab-body">
                        <component :is="currentTabComponent" @complete="renderComplete" :sticky="sticky"></component>
                    </div>
                </div>
            </div>
            <div class="shop-wrapper" v-if="sticky" :class="sticky ? 'sticky' : null">
                <div class="tab">
                    <div class="tab-header">
                        <div class="item" v-for="(item, index) in tabs" :key="index" :class=" current === index ? 'current' : null " @click="tabClick(index)">
                            <span>{{item.title}}</span>
                        </div>
                    </div>
                </div>
            </div>
            <div v-show="sticky" class="wrapper_left" :class="sticky ? 'sticky' : null">
                <LeftMenu ></LeftMenu>
            </div>
            <div class="cart">
                <div class="cart_left">
                    <div class="c">
                        <div class="num">1</div>
                    </div>
                    <div class="p"></div>
                </div>
                <div class="cart_right">未选择商品</div>
            </div>
        </div>
    </div>
</template>

<script>
import BScroll from "@better-scroll/core"
import MouseWheel from '@better-scroll/mouse-wheel'
BScroll.use(MouseWheel)
import mixin from './mixins'

export default {
    name: 'shoppingCart-alipay',
    mixins: [mixin],
    data(){
        return {
            tabs: [
                {id: 1, title: '外卖', component: 'TakeOutFood'},
                {id: 2, title: '到店', component: 'Arrival'},
                {id: 3, title: '评价', component: 'Evaluate'},
                {id: 4, title: '商家', component: 'Details'}
            ],
            current: 0,
            tabHeaderOffsetTop: 0,
            sticky: false
        }
    },
    components: {
        TakeOutFood: () => import("./components/TakeOutFood.vue"),
        Arrival: () => import("./components/Arrival.vue"),
        Evaluate: () => import("./components/Evaluate.vue"),
        Details: () => import("./components/Details.vue"),
        LeftMenu: () => import("./components/Menu"),
    },
    computed: {
        currentTabComponent(){
            return this.tabs[this.current].component
        }
    },
    mounted(){
        this.$nextTick(()=>{
            this.tabHeaderOffsetTop = this.$refs.tabTitle.offsetTop
        })
    },
    methods: {
        renderComplete(){
            this.$nextTick(() => {
                if(this.bscroll){
                    console.log('refresh')
                    this.bscroll.refresh()
                }else{
                    console.log('new')
                    this.bscroll = new BScroll(this.$refs.scroll, {
                        mouseWheel: true,
                        click: true,
                        probeType: 3,
                        bounce: false
                    })
                    this.bscroll.on('scroll', (e)=>{
                        console.log('1245',e)
                        this.sticky = Math.abs(e.y) >= this.tabHeaderOffsetTop
                    })
                }
            })
        },
        tabClick(index){
            this.current = index
        },
        scrollHandle(e){
            console.log(e)
        }
    }
}
</script>

<style lang="less" scoped>
.wrapper_left{
    height: 100%;
    width: 150px;
    background-color: #f0f0f0;
    font-size: 20px;
    color: #666;
    &.sticky{
        position: fixed;
        top: calc(90px + 61px);
        left: 0;
    }
}
    .shop{
        height: 100%;
        overflow: hidden;
        .shop-wrapper{
            &.sticky{
                position: fixed;
                top: 90px;
                left: 0;
                width: 100%;
                &:after{
                    content: '';
                    width: 100%;
                    height: 60px;
                    overflow: hidden;
                    display: block;
                }
            }
            .header{
                position: relative;
                padding: 30px;
                background-color: #fff;
                .bg{
                    position: absolute;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 60%;
                    z-index: 1;
                    background: linear-gradient(to right, #FF7A51, #FF4554);
                }
                .header-wrapper{
                    margin-top: 50px;
                    position: relative;
                    z-index: 2;
                    padding: 30px;
                    border-radius: 25px;
                    box-shadow: 0 0 12px 0 #e5e5e5;
                    background-color: #fff;
                    .logo{
                        position: absolute;
                        right: 30px;
                        top: -30px;
                        width: 90px;
                        height: 90px;
                        border-radius: 10px;
                        background-color: #fff;
                    }
                    .title{
                        font-size: 34px;
                        font-weight: bold;
                        padding-right: 100px;
                    }
                    .time{
                        margin: 40px 0;
                        display: flex;
                        align-items: center;
                        font-size: 20px;
                        .tag{
                            padding: 5px 20px;
                            color: #fff;
                            background-color: #FF4554;
                            border-radius: 30px;
                            margin-right: 20px;
                        }
                    }
                    .address{
                        font-size: 20px;
                        color: #999;
                    }
                }
            }
            .tab{
                background-color: #fff;
                .tab-header{
                    display: flex;
                    border-bottom: 1px solid #eee;
                    
                    .item{
                        flex: 1;
                        font-size: 28px;
                        font-weight: 400;
                        color: #999;
                        display: flex;
                        justify-content: center;
                        height: 60px;
                        overflow: hidden;
                        span{
                            padding-bottom: 20px;
                            position: relative;
                            &:after{
                                content: '';
                                position: absolute;
                                bottom: 0;
                                left: 0;
                                width: 0;
                                height: 5px;
                                border-radius: 3px;
                                background-color: #FF4554;
                                transition: .3s all;
                            }
                        }
                        &.current{
                            span{
                                font-weight: bold;
                                color: #333;
                                &:after{
                                    width: 100%;
                                }
                            }
                        }
                    }
                }
                .tab-body{
                    --cheight: calc(100vh - 90px - 60px - 100px);
                }
            }
        }
        .cart{
            width: 100%;
            height: 100px;
            display: flex;
            position: fixed;
            bottom: 0;
            left: 0;
            background-color: #fff;
            .cart_left{
                border-top: 1px solid #eee;
                flex: 1;
                .c{
                    width: 98px;
                    height: 98px;
                    border-radius: 50%;
                    background: linear-gradient(to right, #FF7A51, #FF4554);
                    margin-left: 30px;
                    margin-top: -20px;
                    position: relative;
                    .num{
                        font-size: 26px;
                        color: #fff;
                        height: 35px;
                        padding: 0 10px;
                        border-radius: 35px;
                        background-color: #FFC600;
                        position: absolute;
                        top: -5px;
                        right: -5px;
                        display: flex;
                        justify-content: center;
                        align-items: center;
                    }
                }
            }
            .cart_right{
                width: 250px;
                height: 100%;
                display: flex;
                justify-content: center;
                align-items: center;
                background-color: #FF4554;
                color: #fff;
            }
        }
    }
</style>